<template>
	<view class="u-wrap">
		<u-sticky bgColor="#fff">
			<u-tabs  :list="listUtabs"
				active-color="#16a5fe" inactive-color="#606266"
				:itemStyle="{width:'175rpx',height: '80rpx'}" 
				:lineHeight="'6rpx'" 
				:lineWidth="'60rpx'"		
			    :current="currentUtabs"
			    @change="onChangeUtabs"
				style="margin-bottom: 15rpx;"
			    >
			</u-tabs>
		</u-sticky>
		
		<view class="u-menu-wrap">
            <scroll-view scroll-y class="right-box" @scrolltolower="onScrolltolower">
                <view class="page-view">
                    <view class="class-item">
                        <view class="item-container">
                          
                            <view class="thumb-box" v-for="(item1, index1) in posterObj.list" :key="index1" @click="gotoDetailOrEdit(item1.id)">
                                <image mode="aspectFill" class="item-image" :src="item1.background"></image>
                                <view class="item-name text-2line-ellipsis">{{item1.name}}</view>
                            </view>
                        </view>
                    </view>
                </view>
            </scroll-view>
			
		</view>
	</view>
</template>

<script>   
    let that = null
    const api = require("@/static/js/api.js")
    const utils = require("@/static/js/utils.js")
    const posterTypeId = [
       'material','article','activity', 'card'
    ];
        
    
	export default {
		data() {
			return {
                //==侧边=============================================
				posterObj: {
                    // type: ['名片', '文章', '活动'],
                    list: [],
                    loadStatus: "loading",
                    pageIndex: 1,
					curPageIndex: 0,
                },
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
                //==侧边 end==========================================
                //===顶部tab==========================================
                listUtabs: [ {
                    name: '物料海报'
                },{
                    name: '文章海报'
                },{
                    name: '活动海报'
                },
                  {
                    name: '名片海报'
                  },],
                currentUtabs: 0,
                //===顶部tab end======================================
			}
		},
        onLoad() {
            that = this
        },
		computed: {
			
		},
        onShow() {
            that.clearListShow(that.posterObj)
            that.getPosterList()
        },
        onReachBottom() {
            that.getPosterList()
        },
        
		methods: {
            onScrolltolower(){
                that.getPosterList()
            },
            gotoDetailOrEdit(id){
				uni.navigateTo({
				    url:"./detail?id="+id+"&type="+that.current
				})
                // if (that.currentUtabs == 0){
                //     uni.navigateTo({
                //         url:"./detail?id="+id+"&type="+that.current
                //     })
                // }else{
                //     uni.navigateTo({
                //         url:"./editPoster?id="+id+"&type="+that.current
                //     })
                // }
            },
            addPoster(){
                uni.navigateTo({
                    url:"./editPoster?&type="+that.current
                })    
            },
            getPosterList(){
				if (that.posterObj.curPageIndex != that.posterObj.pageIndex){
					that.posterObj.curPageIndex = that.posterObj.pageIndex
					api.getPosterList({
					    interfaceName: posterTypeId[that.currentUtabs],
					    param: {
					        limit: 10,
					        page: that.posterObj.pageIndex,
					    }
					}).then(res => {
					    console.log(res)
					    if (res.code==0) {
					        that.pushListShow(that.posterObj, res.data)
					    }
					})
				}
           
            },
            
            /**
             * @description: 列表中添加数据
             * @param {number} id   u-tabs-swiper的当前ID
             * @param {list}  列表
             * @return void
             */
            pushListShow(srcObj, pushlist) {
                if (pushlist && pushlist.length > 0) {
                    for (let i = 0; i < pushlist.length; i++) {
                        pushlist[i].createTime = utils.formatTime(pushlist[i].createTime, 'yyyy-MM-dd')
                        srcObj.list.push(pushlist[i])
                    }
                    srcObj.pageIndex++;
            
                    if (pushlist.length == 10) {
                        srcObj.loadStatus = "loadmore"
                    } else {
                        srcObj.loadStatus = "nomore"
                    }
                } else {
                    srcObj.loadStatus = "nomore"
                }
            },
            
            //----清空列表
            clearListShow(srcObj) {
				srcObj.curPageIndex = 0;
                srcObj.pageIndex = 1;
                srcObj.list.splice(0, srcObj.list.length)
            },
            
            
            
            
            //===顶部tab
            onChangeUtabs(item) {
				var index = item.index
                if (this.currentUtabs != index){
                    this.currentUtabs = index;
                    that.clearListShow(that.posterObj)
                    that.getPosterList()
                }
            },
            //----侧边栏
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if(index == this.current) return ;
				this.current = index;
                
                that.clearListShow(that.posterObj)
                that.getPosterList()
                
				// 如果为0，意味着尚未初始化
				if(this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({size: true},res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if(!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return ;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>
<style>
    page{
        background-color: #f3f3f3;
    }
    
</style>
<style lang="scss" scoped>

	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 160rpx;
		height: 100%;
        background-color: #FFFFFF;
	}

	.u-tab-item {
		height: 110rpx;
		background: #FFFFFF;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #16a5fe;
		font-size: 30rpx;
		font-weight: 600;
		background: #f3f3f3;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #16a5fe;
		height: 32rpx;
		left: 0;
		top: 39rpx;
        margin-left: 15rpx;
	}

	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
		// background-color: rgb(250, 250, 250);
	}
	
	.page-view {
		// padding: 16rpx;
	}
	
	.class-item {
		margin-bottom: 30rpx;
		// padding: 16rpx;
		border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}
	
	.thumb-box {
        margin-left: 30rpx;
        margin-top: 25rpx;
        width: 330rpx;
        background-color: #ffffff;
        border-radius: 10rpx;
        overflow: hidden;
        .item-image {
        	width: 100%;
        	height: 450rpx;
        }
        .item-name {
            width: 100%;
            padding: 0 20rpx;
            height: 82rpx;
            font-size: 28rpx;
            color: #333333;
            line-height: 40rpx;
            display: flex;
            align-items: center;
            justify-content: center;
        }
	}
	.addPoster{
        margin-left: 30rpx;
        margin-top: 25rpx;
        width: 260rpx;
        height: 472rpx;
        background-color: #ffffff;
        border-radius: 10rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        font-size: 24rpx;
        color: #999999;
        .icon{
            width: 91rpx;
            height: 91rpx;
            margin-bottom: 27rpx;
        }
    }
    
    

    
    
</style>
